import {Command} from './constant';
import _ from 'lodash';
import {IMainReducer} from '../types';
import {Action} from 'typings';
import produce from 'immer';
import * as immerUtil from '@/redux/immer-util';

const INITIAL_STATE: IMainReducer = {
  //会员基本信息
  store: {},
  // 会员的当前等级
  level: {},
  // 不同等级的折扣信息
  levelList: [],
  userInfo: {}, //用户信息
  growthValueIsOpen: false, //是否打开成长值
  //二维码弹窗是否打开
  erweimaState: false,
  //是否展示评价相关信息
  isShow: false,
  // 轮播显示的图片
  showImages: {images: [], idx: 0},
  pvUvStaticsNum: 0,
};

export default function main(state = INITIAL_STATE, action: Action): IMainReducer {
  const {type, payload} = action;

  return produce<IMainReducer>(state, (draftState) => {
    switch (type) {
      //通用改变数据
      case Command.commonChange:
        return immerUtil.commonChange(draftState, {...payload, key: 'main'});

      //初始化
      case Command.init:
        for (let propKey in payload.main) {
          //@ts-ignore 这里处理的不够好.
          draftState[propKey] = payload.main[propKey];
        }
        return draftState;

      //重置
      case Command.clean:
        for (let propKey in INITIAL_STATE) {
          //@ts-ignore 这里处理的不够好.
          draftState[propKey] = INITIAL_STATE[propKey];
        }
        return draftState;
    }
  });
}

//create by moon https://github.com/creasy2010/moon
